package com.wnhz.work.jy.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;

import com.wnhz.work.jy.R;
import com.wnhz.work.jy.activity.GoodDetailsMainActivity;
import com.wnhz.work.jy.adapter.GridViewAdapter;
import com.wnhz.work.jy.bean.HomeFragment1.RecommendBean;
import com.wnhz.work.jy.bean.HomefragBean;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * ----推荐
 */
public class RecommendFragment extends Fragment {
    private View view;

    private GridView gridView;
    private int screenWidth;
    private List<RecommendBean> homeList = new ArrayList<>();
    private GridViewAdapter mAdapter;


    private LinearLayout linear;


    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    mAdapter.notifyDataSetChanged();
                    break;

                default:
                    break;
            }

            super.handleMessage(msg);
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_recommend, container, false);
        initView();
        return view;
    }



    private void initView() {

        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        screenWidth = dm.widthPixels;

        linear = (LinearLayout) view.findViewById(R.id.ll_recommend);

        addGridView(linear);

    }

    private void addGridView(LinearLayout linear) {
        View view1 = getActivity().getLayoutInflater().inflate(R.layout.addgridview1, null);
        gridView = (GridView) view1.findViewById(R.id.gridView);

      /*  GridLayoutManager manager = new GridLayoutManager(getActivity(), 2);
        manager.setOrientation(LinearLayoutManager.VERTICAL);*/

        mAdapter = new GridViewAdapter(getActivity(), homeList, screenWidth);
        gridView.setAdapter(mAdapter);

//        mHandler.sendEmptyMessage(1);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), GoodDetailsMainActivity.class);
                startActivity(intent);
            }
        });
        linear.addView(view1);

    }


}
